/*
 * Copyright (c) 2013, Ivan Babushkin.
 */
package asngen.codec;

import asngen.core.Decoder;
import java.io.*;

public final class MderDecoder implements Decoder {
    private InputStream is;
    private DataInputStream dis;

    public MderDecoder(InputStream is) {
        this.is = is;
        dis = new DataInputStream(is);
    }

//    @Override
//    public long decodeInteger(int length) throws IOException {
//        long value;
//        switch (length) {
//            case 1: value = dis.readByte(); break;
//            case 2: value = dis.readShort(); break;
//            case 3:
//            case 4: value = dis.readInt(); break;
//            default: throw new UnsupportedOperationException();
//        }
//        return value;
//    }

    public long decodeInteger(long l, long r) throws IOException {
        return new IntegerDecoder(l, r).decode();
    }

    private class IntegerDecoder extends IntegerDecoderRuntime {
        public IntegerDecoder(long l, long r) {
            super(dis, l, r);
        }

        public long decode() throws IOException {
            if (boundedWithNegatives()) {
                fixedToMax();
                return twosComplement();
            }
            if (boundedWithoutNegatives()) {
                fixedToMax();
                return positiveInt();
            }
            throw new UnsupportedOperationException();
        }
    }
}
